<!--
 * @Author: Lzx
 * @Date: 2022-04-17 23:41:12
 * @LastEditors: Lzx
 * @LastEditTime: 2022-04-21 22:18:22
 * @FilePath: /web-vue/src/views/SysRole/dataScopeForm.vue
 * @Description: 角色数据域
-->
<template>
  <div>
    <!-- 弹出层-表单 -->
    <el-dialog
      class="edit-dialog"
      title="角色授权数据域"
      :visible.sync="visible"
      :confirmLoading="confirmLoading"
      top="7vh"
    >
      <el-form
        :model="editForm"
        :rules="rules"
        ref="editForm"
        status-icon
        label-width="120px"
        style="width: 100%"
        class="layer-form-style"
      >
        <el-form-item label="数据域：" prop="dataScope" size="mini">
          <el-select
            v-model="editForm.dataScope"
            filterable
            clearable
            placeholder="请选择数据域"
            style="width: 99%"
          >
            <el-option
              v-for="item in $store.state.DICT.dataScopeSelect"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="影响角色：">
          <el-input
            type="textarea"
            v-model="tipInfo"
            :autosize="{ minRows: 3, maxRows: 9 }"
            readonly
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          type="primary"
          icon="el-icon-finished"
          @click="handleSubmit()"
          >保存
        </el-button>
        <el-button icon="el-icon-refresh-left" @click="handleCancel()"
          >取消
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      confirmLoading: false,
      editForm: {
        ids: null,
        dataScope: null,
      },
      tipInfo: null,
      // 表单校验规则
      rules: {
        dataScope: [
          {
            required: true,
            message: "请选择角色数据域！",
            trigger: "blur",
          },
        ],
      },
    };
  },

  methods: {
    // 打开【角色】表单编辑窗口
    openEditForm(isShow, items) {
      this.visible = isShow;
      // 【角色】编辑信息填充（延迟执行，确保子组件初始化）
      setTimeout(() => {
        // 赋值
        this.editForm.ids = items.map((m) => m.roleId);
        this.tipInfo = items.map((m) => m.role).toString();
        if (items.length == 1) {
          this.editForm.dataScope = items[0]["dataScope"];
        }

        // 清除表单校验结果
        this.$refs["editForm"].clearValidate();
      }, 1);
    },

    // 关闭【角色】表单编辑窗口
    handleCancel() {
      this.visible = false;
      this.$refs["editForm"].resetFields();
    },

    // 提交
    handleSubmit() {
      this.$refs["editForm"].validate((valid) => {
        if (valid) {
          const loading = this.$loading({
            target: document.querySelector(".edit-dialog"),
            lock: true,
            text: "正在保存，请耐心等待！",
          });

          // 请求参数
          let param = Object.assign({}, this.editForm);
          // 发起请求
          this.$api
            .authDataScope(param)
            .then((res) => {
              loading.close();
              let { status, message, data } = res.data; // 响应数据
              if (status == 200) {
                this.$emit("afterSubmit");
                this.handleCancel();
                // 提示信息
                this.$message({
                  type: "success",
                  message: `操作成功！${message}`,
                  duration: 1680,
                });
              } else if (status == 403) {
                this.$message({
                  message: `权限不足！${message}`,
                  type: "error",
                  center: true,
                  duration: 1680,
                });
                // this.$router.push("/login");
              } else {
                this.$alert(`操作失败！${message}`, "【角色授权数据域】", {
                  type: "error",
                  confirmButtonText: "确定",
                  callback: (action) => {},
                });
              }
            })
            .catch((err) => {
              loading.close();
              this.visible = false;
              console.log(`角色授权数据域接口发生错误：${err}`);
            });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
// 表单布局样式
@import "@/assets/css/form-layout.scss";
</style>
